'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 离开战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var pvpConf = require('../../../../config/pvpConf');
var heroConf = require('../../../../config/heroConf');
var roleService = require('../../../../services/roleService');
var rewardType = require('../../../../consts/rewardType');
var rewardService = require('../../../../services/rewardService');
var dataApi = require('../../../../libs/dataApi');
var eventService = require('../../../../services/eventService');
var buildType = require('../../../../consts/buildType');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var road_id = msg.road_id;
    if (!road_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var event_reward_list = [];
    var event_star = 0;
    var success = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['pvp', 'role', 'build', 'soldier', 'accrued'],
                    cb
                );
            },
            // jshint maxstatements:50
            function (cb) {
                var pvp_model = player_manager.pvp;
                var role_model = player_manager.role;
                var soldier_model = player_manager.soldier;
                var build_model = player_manager.build;

                // 判断是否是随机事件
                var road_event = pvp_model.getRoadEvent(road_id);
                if (!!road_event) {
                    // 判断人口是否满足
                    var nexus_build = build_model.getBuildByType(buildType.NEXUS);
                    var need_soldier_num = dataApi.event_population.findById(nexus_build.getLv());
                    var soldier_total_num = 0;
                    _.each(soldier_model.select_soldier_list, function (select_soldier) {
                        soldier_total_num += soldier_model.getProductedSoldierNum(select_soldier);
                    });

                    if (soldier_total_num < need_soldier_num) {
                        event_star = 0;
                    } else {
                        event_star = eventService.getStar(road_event.event_type, role_model, soldier_model);
                    }

                    if (event_star > 0) {
                        var event_star_effect_conf = dataApi.event_star_effect.findById(event_star);
                        if (_.random(0, 100) <= event_star_effect_conf.probability) {
                            success = 1;

                            event_reward_list = dataApi.event_reward.findById(road_event.event_type);
                            rewardService.addRewardListToRole(player_manager, event_reward_list, moduleType.MDL_PVP, operationType.OPR_PVP_RANDOM_EVENT_REWARD);

                            var lose_per = _.random(event_star_effect_conf.lose_min_per, event_star_effect_conf.lose_max_per);
                            _.each(soldier_model.select_soldier_list, function (select_soldier) {
                                var sub_num = soldier_model.getProductedSoldierNum(select_soldier) * lose_per / 100;
                                soldier_model.subProductedSoldierNum(select_soldier, sub_num);
                            });
                        }
                    }

                    pvp_model.removeRoadEvent(road_event);
                }

                player_manager.save(cb);
        }],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    event_reward_list: event_reward_list,
                    event_star: event_star,
                    success: success
                }
            });
        }
    );
};
